pp108 : Regular Expression changes for Multi-browser Support

Regular Expression changes for Multi-browser Support

This topic describes the changes needed in Regular Expressions to enable multi-browser support.

In order to be able to work with applications in multiple browsers, all content types in the application must be Standards compliant.

Regular expressions in an application are constructed in the following ways:

  • Using a regular expression literal, such as var re = /ab+c/;.
  • Calling the constructor function of the RegExp object, such as var re = new RegExp("ab+c");.

Different browsers behave differently when using regular expressions. In the Internet Explorer browser, a new RegExp object is created each time the regular expression is used, while in other browsers a global RegExp object is created that is used each time. This can result in issues in the search functionality of the browsers.

To avoid such issues,

  • always create a new RegExp object.
  • use a global RegExp, and reset the property regexp.lastIndex = 0 every time before calling exec().
  • avoid using the /g flag (global search) unless really needed.